const users = require('../models/users')
const usersModel = require('../models/users')
const { genHash, compare, sign, verify } = require('../utils/tools')

// 应用中间件
const signup = async function(req, res, next) {
  res.set('Content-Type', 'application/json; charset=utf-8')

  let {username, password, filename} = req.body

  // 判断用户是否存在
  let findRes = await usersModel.find(username)

  if (findRes) {
    res.render('fail', {
      data: JSON.stringify({
        msg: '用户名已存在.'
      })
    })
  } else {
    // 注册用户
    const hash = await genHash(password)
    
    let signupRes = await usersModel.signup({
      username,
      password: hash,
      filename
    })

    if (signupRes) {
      res.render('succ', {
        data: JSON.stringify({
          msg: '注册成功.'
        })
      })
    } else {
      res.render('fail', {
        data: JSON.stringify({
          msg: '注册失败.'
        })
      })
    }
  }
}

const signin = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')

  let { username, password } = req.body

  let findRes = await usersModel.find(username)

  if (findRes) {
    let result = await compare(password, findRes.password)

    if (result) {
      // 保存session
      // req.session.username = username
      let token = sign(username)
      res.set('X-ACCESS-TOKEN', token)

      res.render('succ', {
        data: JSON.stringify({
          msg: '登录成功.'
        })
      })
    } else {
      res.render('fail', {
        data: JSON.stringify({
          msg: '用户名或密码错误.'
        })
      })
    }
  } else {
    res.render('fail', {
      data: JSON.stringify({
        msg: '用户名或密码错误.'
      })
    })
  }
}

const update = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  let updateRes = await usersModel.update(req.body)
  // console.log(updateRes)
  if (updateRes) {
    res.render('succ', {
      data: JSON.stringify({
        msg: '修改成功.'
      })
    })
  } else {
    res.render('fail', {
      data: JSON.stringify({
        msg: '修改失败.'
      })
    })
  }
}

const auth = (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')

  let token = req.headers['x-access-token']

  let decoded = verify(token)

  if (decoded) {
    res.render('succ', {
      data: JSON.stringify({
        msg: '权限验证通过.'
      })
    })
  } else {
    res.render('fail', {
      data: JSON.stringify({
        msg: '请登录.'
      })
    })
  }
}

const list = async (req, res, next) => {
  let result = await usersModel.list()

  res.render('succ', {
    data: JSON.stringify(result)
  })
}

const remove = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  let removeRes = await usersModel.remove(req.body)
  if (removeRes) {
    res.render('succ', {
      data: JSON.stringify({
        msg: '删除成功.'
      })
    })
  } else {
    res.render('fail', {
      data: JSON.stringify({
        msg: '删除失败.'
      })
    })
  }
}

const upload = async (req, res, next) => {
  const filename = req.body.filename
  if (filename) {
    res.render('succ', {
      data: JSON.stringify({
        filename
      })
    })
  } else {
    res.render('fail', {
      data: JSON.stringify({
        msg: '图片上传失败.'
      })
    })
  }
}

module.exports = {
  signup,
  signin,
  auth,
  list,
  update,
  remove,
  upload
}